home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************/
- /* "Kid's Clicks" */
- /* (v1.0.1) */
- /* Original version created: July 1995 */
- /* */
- /* For Allie */
- /* */
- /* Created By Eric Long - AOL: EricMLong */
- /* CIS: 72764,1072 */
- /*******************************************************/
-
- /* This program is pretty much a plain old modeless dialog. It will display a
- picture and allow users to click on places in the picture, defined by user
- items, to hear corresponding sounds played. The picture will be scaled to
- full screen size, up to 640 x 480. If the resolution of the screen is
- greater, the window will be sized to 640 x 480 and centered, revealing a
- title bar and close box.
-
- Kid's Clicks requires system seven; it wouldn't be hard to make it 6 compatible,
- but I'm not real keen on doing it. How long should 6 be kept on the respirator?
- I set up a requirement for 256 colors, which also isn't really necessary, but I
- felt it was reasonable and helps eliminate a lot of hassles with modules.
-
- */
-
- /* Version History:
-
- 1.0.1 Corrections -
- CheckSystemEnvironment Routine Now Works with 16bit monitors.
-
- 1.0.2 Corrections -
- AboutModuleFilter() error corrected setting port with temp variable
-
- Changes -
- New routine added: GetNew_Module_CDialog.
- Modified Install_KidsModule() and Adjust_Module_ToFitScreen()
- to fit with new routine.
-
- Now users running in 256 colors will see dramatic image quality
- improvement with PICTs that do not use standard system colors.
- This routine was derived from example source code on the DEVELOP
- CD, by Nick Thompson of Apple.
-
- Added watch cursor while loading modules. See Open_KidsModule().
-
- KC memory allocation increased to 1050K from 700K.
- This overcomes a not-so-bright idea I introduced into modules,
- by using the module's PICT in the module About screen.
- Why is that not so bright? Because it causes KC to need a lot
- more memory, or it begins to corrupt itself after users select
- "About the module…". Mea Culpa.
-
- */
-
-
- #include <AppleEvents.h>
- #include <Sound.h>
- #include <PictUtil.h>
- #include <Palettes.h>
- #include <QDOffscreen.h>
- #include <GestaltEqu.h>
- #include <string.h>
- #include <stdio.h>
-
- /* konstants */
-
- #define kKidsClicksSig 'Kydz'
- #define kThisVersion 1
-
- #define kBaseResID 128
- #define rErrorAlertID 501
- #define rWrongVersionAlert 502
- #define rErrStringsID 128
- #define eCantOpenMultiModules 1
- #define eNotAKidsModule 2
- #define eUnableToOpenFile 3
- #define eWrongSysEnvirons 4
- #define eNotEnoughMemory 5
-
- #define kAboutScreenFilter (ProcPtr)&AboutScreenFilter
- #define rAboutSnd 500
- #define kAboutDlgMem sizeof(DialogRecord)
- #define rAboutDlgID 500
- #define iAllieFrame 2
- #define rAlliePictID 1000
-
- #define kKidsModuleType 'Kmod'
- #define kModuleBaseResID 5000
- #define rAboutModuleID 1000
- #define rExpectedPICTID 5000
- #define rExpectedSndID 9001
-
- #define kSleep 20L
- #define sEmptyString "\p"
- #define kSystemSeven 0x00000700L
- #define k256ColorDepth 8
-
- /* Window Stuff */
- #define kMoveToFront (WindowPtr)-1L
-
- /* Menus */
- #define mApple kBaseResID
- #define iAbout 1
- #define iAboutModule 2
-
- #define mFile kBaseResID+1
- #define iOpen 1
- #define iClose 2
- #define iQuit 4
-
- #define mScramble kBaseResID+2
- #define iScramble 1
- #define iUnscramble 2
-
- #define mDirections kBaseResID+3
- #define rDirectionStrings 129
- #define sOpenModule 1
- #define sClickMouse 2
-
-
- /* Module port Size Info */
- #define kMaxModuleWidth 640
- #define kMaxModuleHeight 480
-
- const RGBColor kRGBBlack = {0, 0, 0};
- const RGBColor kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
-
-
- typedef struct DLOGRsrcRec
- {
- Rect boundsRect;
- short procID;
- char visible;
- char filler;
- char goAwayFlag;
- char filler2;
- long refCon;
- Str255 title;
- }DLOGRsrcRec, *DLOGRsrcPtr, **DLOGRsrcHandle;
-
-
- /************************* Globals ******************************/
-
- Boolean gModuleIsLoaded, gDone, gFatal, gSoundsAreScrambled;
- DialogPtr gModuleDialog;
- short gKidsModuleResNum, gSoundOrder[255];
- PaletteHandle gPICTPalette;
- CTabHandle gPICTCTab;
-
-
- /************************ Prototypes **********************************/
-
- void main( void );
- void ToolBoxInit ( void );
- void AEInit( void );
- void MenuBarInit( void );
- void EventLoop ( void );
- void DoEvent( EventRecord *eventPtr );
- Boolean DoUpdate( WindowPtr window );
- void HandleNull( EventRecord *event );
- void HandleKeyDown(EventRecord *eventPtr);
- void HandleMouseDown (EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void HandleScrambleChoice( short item );
- void Get_KidsModule( void );
- void Open_KidsModule( FSSpec *kidsModule );
- Boolean Verify_KidsModule(short myResNum);
- Boolean Verify_Resource( OSType rType, short rID );
- void Check_KCModuleVersion( void );
- OSErr Install_KidsModule( short myResNum );
- OSErr GetNew_Module_CDialog( void );
- void Adjust_Module_ToFitScreen( DialogPtr dialog );
- void Close_KidsModule( void );
- void Handle_KidsClicks( EventRecord *eventPtr );
- void PlaySound( short whichSound );
- void ScrambleSoundOrder( void );
- void Do_AboutDialog( void );
- void Do_AboutModuleDlg( void );
- pascal Boolean AboutScreenFilter( DialogPtr dialog, EventRecord *event,
- short *itemHit );
- pascal void rectProc( DialogPtr theDialog, short item );
-
- /* Apple Events Required Suite */
- pascal OSErr DoOpenApp( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon );
- pascal OSErr DoOpenDoc( const AppleEvent *theAppleEvent,
- AppleEvent *reply, long refCon );
- pascal OSErr DoPrintDoc( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon );
- pascal OSErr DoQuitApp( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon );
-
- void DoError( short err );
- Boolean CheckSystemEnvironment( void );
- void Pstrcpy( Str255 *destString, Str255 srcString);
-
-
- /******************** main ***********************************/
-
- void main ( void )
- {
- ToolBoxInit ();
- MaxApplZone();
- if (!CheckSystemEnvironment())
- {
- gFatal = true;
- DoError(eWrongSysEnvirons);
- }
- else
- {
- MenuBarInit();
- AEInit();
-
- FlushEvents( everyEvent, 0 );
- EventLoop();
- }
- }
-
-
- /************************* ToolBoxInit ******************************/
-
- void ToolBoxInit ( void )
- {
- InitGraf ( &thePort );
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( nil );
- InitCursor ();
- }
-
-
- /*********************** AEInit ***************************/
-
- void AEInit( void )
- {
- OSErr ignoreErr;
-
- ignoreErr = AEInstallEventHandler ( kCoreEventClass,
- kAEOpenApplication, DoOpenApp, 0L, false );
-
- ignoreErr = AEInstallEventHandler ( kCoreEventClass,
- kAEOpenDocuments, DoOpenDoc, 0L, false );
-
- ignoreErr = AEInstallEventHandler ( kCoreEventClass,
- kAEPrintDocuments, DoPrintDoc, 0L, false );
-
- ignoreErr = AEInstallEventHandler ( kCoreEventClass,
- kAEQuitApplication, DoQuitApp, 0L, false );
- }
-
-
- /*********************** MenuBarInit ***************************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
- Str255 directionStr;
-
- menuBar = GetNewMBar( kBaseResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
-
- menu = GetMHandle(mDirections);
- GetIndString( directionStr, rDirectionStrings, sOpenModule );
- SetItem(menu, 1, directionStr);
- }
-
-
- /************************ EventLoop ************************/
-
- void EventLoop ( void )
- {
- EventRecord event;
-
- gModuleIsLoaded = gDone = gFatal = gSoundsAreScrambled = false;
-
- while ( !gDone )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, nil) )
- DoEvent ( &event );
- else
- HandleNull( &event );
- }
- }
-
-
- /************************** DoEvent ***********************/
-
- void DoEvent( EventRecord *eventPtr )
- {
- switch ( eventPtr->what )
- {
- case kHighLevelEvent:
- AEProcessAppleEvent( eventPtr );
- break;
- case keyDown:
- case autoKey:
- HandleKeyDown(eventPtr);
- break;
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case updateEvt:
- DoUpdate((WindowPtr)eventPtr->message);
- break;
- }
- }
-
-
- /********************* DoUpdate ******************************/
-
- Boolean DoUpdate( WindowPtr window )
- {
- Boolean isMyWindow = false;
-
- isMyWindow = (window == (WindowPtr)gModuleDialog);
- if (isMyWindow)
- {
- BeginUpdate( window );
- UpdtDialog((DialogPtr)window, window->visRgn);
- EndUpdate( window );
- }
-
- return (isMyWindow);
- }
-
-
- /******************* HandleNull **************************/
-
- void HandleNull( EventRecord *eventPtr )
- {
- ;
- }
-
-
- /******************** HandleKeyDown ****************************/
-
- void HandleKeyDown(EventRecord *eventPtr)
- {
- char theChar;
-
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- }
-
-
- /******************* HandleMouseDown **************************/
-
- void HandleMouseDown ( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow ( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick ( eventPtr, window );
- break;
- case inDrag :
- DragWindow ( window, eventPtr->where,
- &screenBits.bounds );
- break;
- case inContent:
- Handle_KidsClicks(eventPtr);
- break;
- case inGoAway:
- Close_KidsModule();
- break;
- }
- }
-
-
- /********************** HandleMenuChoice **************************/
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- case mScramble:
- HandleScrambleChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /********************* HandleAppleChoice ***************************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- Do_AboutDialog();
- break;
- case iAboutModule:
- Do_AboutModuleDlg();
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /********************* HandleFileChoice ****************************/
-
- void HandleFileChoice( short item )
- {
- switch(item)
- {
- case iOpen:
- Get_KidsModule();
- break;
- case iClose:
- Close_KidsModule();
- break;
- case iQuit:
- if (gModuleIsLoaded)
- Close_KidsModule();
- gDone = true;
- break;
- }
- }
-
-
- /******************** HandleScrambleChoice ***************************/
-
- void HandleScrambleChoice( short item )
- {
- MenuHandle menu;
-
- switch(item)
- {
- case iScramble:
- ScrambleSoundOrder();
- break;
- case iUnscramble:
- gSoundsAreScrambled = false;
- menu = GetMHandle(mScramble);
- DisableItem(menu, iUnscramble);
- break;
- }
- }
-
-
- /*********************** Get_KidsModule ******************************/
-
- void Get_KidsModule( void )
- {
- FSSpec kidsModule;
- SFReply reply;
- SFTypeList fileList;
- Point where;
-
- fileList[0] = kKidsModuleType;
- where.h = where.v = 100;
- SFGetFile( where, nil, nil, 1, fileList, nil, &reply);
- if (reply.good)
- {
- FSMakeFSSpec( reply.vRefNum, 0, reply.fName, &kidsModule );
- Open_KidsModule( &kidsModule );
- }
- }
-
-
- /************************ Open_KidsModule ***************************/
-
- void Open_KidsModule( FSSpec *kidsModule )
- {
- short myResNum, err;
- FInfo moduleInfo;
- CursHandle waitCursH;
- Cursor waitCursor;
-
- waitCursH = GetCursor(watchCursor);
- HLock((Handle)waitCursH);
- waitCursor = **waitCursH;
- HUnlock(waitCursH);
- ReleaseResource(waitCursH);
-
- SetCursor(&waitCursor); // put up the watch cursor
-
- err = HGetFInfo( kidsModule->vRefNum, kidsModule->parID,
- kidsModule->name, &moduleInfo );
- if (err)
- {
- DoError(eUnableToOpenFile);
- goto cantDoIt;
- } // System Error
-
- /* Make sure it's our document type */
- if ((moduleInfo.fdType != kKidsModuleType)
- || (moduleInfo.fdCreator != kKidsClicksSig))
- {
- DoError(eNotAKidsModule);
- goto cantDoIt;
- }
-
- /* Open it */
- myResNum = HOpenResFile(kidsModule->vRefNum, kidsModule->parID,
- kidsModule->name, fsCurPerm);
-
- if (Verify_KidsModule(myResNum))
- {
- if (gModuleIsLoaded)
- Close_KidsModule();
- err = Install_KidsModule(myResNum);
- if (err)
- {
- DoError(err);
- goto cantDoIt;
- gModuleIsLoaded = false;
- }
- gKidsModuleResNum = myResNum;
- gModuleIsLoaded = true;
- Check_KCModuleVersion();
- }
- else
- {
- CloseResFile(myResNum);
- DoError(eNotAKidsModule);
- }
-
- cantDoIt:
- SetCursor(&arrow);
- }
-
-
- /*********************** Verify_KidsModule ****************************/
-
- Boolean Verify_KidsModule(short myResNum)
- {
- Boolean itIs = false;
- Handle myResource;
-
- // Check out the data in the module file, see if it appears to actually
- // be a Kid's Clicks module
-
- itIs = Verify_Resource('PICT', rExpectedPICTID); // Screen PICT
- itIs = Verify_Resource('DLOG', kModuleBaseResID); // The Dialog
- itIs = Verify_Resource('snd ', rExpectedSndID); // Make sure there's sounds
- itIs = Verify_Resource('STR ', kModuleBaseResID); // About… string for module
- itIs = Verify_Resource('ALRT', rAboutModuleID); // About… Dialog
- itIs = Verify_Resource('vers', kBaseResID); // Module Designed For Version#
-
- // If itIs still is after those tests, I'm convinced.
- return(itIs);
- }
-
-
- /************************ Verify_Resource ******************************/
-
- Boolean Verify_Resource( OSType rType, short rID )
- {
- Boolean resOK = false;
- Handle rHandle;
-
- // Verify existance of resource
- rHandle = GetResource( rType, rID );
- if (rHandle)
- resOK = true;
- ReleaseResource(rHandle);
-
- return(resOK);
- }
-
-
- /************************ Check_KCModuleVersion ******************************/
-
- void Check_KCModuleVersion( void )
- {
- Handle versHandle;
- short version = 1;
- Str255 versString;
-
- // This routine looks to see if the module is meant for a newer version
- // of the app than this version. If so, an alert tells the user and
- // notifies them that some features of the module might not work
- // if the present version of Kid's Clicks doesn't handle those features.
-
- versHandle = GetResource('vers', kBaseResID);
- if (versHandle)
- version = **versHandle;
- ReleaseResource(versHandle);
-
- if (version > kThisVersion)
- {
- PtoCstr(versString);
- sprintf(versString, "%d.0", version);
- CtoPstr(versString);
- ParamText(versString, sEmptyString, sEmptyString, sEmptyString);
- NoteAlert(rWrongVersionAlert, nil);
- }
-
- }
-
-
- /********************* Install_KidsModule **************************/
-
- OSErr Install_KidsModule( short myResNum )
- {
- StringHandle strHandle;
- Handle menu;
- Str255 aboutString;
- OSErr myErr = 0;
- Str255 directionStr;
- GWorldPtr theNewWorld;
-
- myErr = GetNew_Module_CDialog();
- if (! gModuleDialog)
- goto moduleIncomplete;
-
- SetPort(gModuleDialog);
- Adjust_Module_ToFitScreen(gModuleDialog);
- ShowWindow(gModuleDialog);
-
- // All this for the module "About" String
- strHandle = GetString(kModuleBaseResID);
- HLock(strHandle);
- Pstrcpy(aboutString, *strHandle);
- HUnlock(strHandle);
- ReleaseResource(strHandle);
-
- menu = GetMHandle(mApple);
- InsMenuItem(menu, aboutString, 1);
-
- menu = GetMHandle(mFile);
- EnableItem(menu, iClose);
-
- menu = GetMHandle(mScramble);
- EnableItem(menu , 0);
- EnableItem(menu, iScramble);
- DrawMenuBar();
-
- menu = GetMHandle(mDirections);
- GetIndString(directionStr, rDirectionStrings, sClickMouse);
- SetItem(menu, 1, directionStr);
-
- moduleIncomplete:
- return(myErr);
- }
-
-
- /************************ GetNew_Module_CDialog **************************/
-
- OSErr GetNew_Module_CDialog( void )
- {
- OSErr theErr = 0;
- Handle module_itemList;
- DLOGRsrcHandle module_DLOG;
- Rect theRect;
- CGrafPtr savedPort;
- GWorldPtr savedGWorld;
- GWorldPtr theNewWorld;
- GDHandle oldDevice;
- PicHandle modulePicture;
- PictInfo thePictInfo;
-
-
- /* To make the picture look pretty, we'll set the color environment to suit */
-
- modulePicture = GetPicture(kModuleBaseResID); // Get the module's picture
- if (!modulePicture)
- {
- theErr = eUnableToOpenFile;
- goto end_GetNew_Module_CDialog;
- }
-
- /* Create an offscreen gWorld with the color environment needed */
-
- // Use Rect from the module's DLOG resource
- module_DLOG = GetResource('DLOG', kModuleBaseResID);
- theRect = (**module_DLOG).boundsRect;
- ReleaseResource(module_DLOG);
-
- // Set up a palette and color table from module picture
- theErr = GetPictInfo( modulePicture, &thePictInfo, returnColorTable, 256, systemMethod, 0);
- ReleaseResource(modulePicture); // Let it go, we don't need it anymore
-
- gPICTPalette = NewPalette( 256, thePictInfo.theColorTable, pmTolerant, 0x1000 );
- gPICTCTab = thePictInfo.theColorTable;
-
- // Create the offscreen gWorld, 8 bits deep (256 Colors)
- theErr = NewGWorld( &theNewWorld, 8, &theRect, gPICTCTab, nil, 0L );
- if( theErr != noErr )
- {
- theErr = eNotEnoughMemory;
- goto end_GetNew_Module_CDialog;
- }
-
- // Save the current gWorld
- GetGWorld( &savedPort, &oldDevice );
-
- // Initialize some parameters of the offscreen gWorld
- SetGWorld( theNewWorld, nil );
- RGBForeColor( &kRGBBlack ); // Ensure the fg and bg colors are
- RGBBackColor( &kRGBWhite ); // as anticipated.
- PenMode( srcCopy ); // Ensure the transfer mode is as expected.
-
- // Restore the saved gWorld
- SetGWorld(savedPort, oldDevice);
-
-
- /* Using our offscreen gWorld, we will create a new color dialog for the module */
-
- // Load the module itemlist
- module_itemList = GetResource('DITL', kModuleBaseResID);
- if (! module_itemList)
- {
- theErr = eUnableToOpenFile;
- goto end_GetNew_Module_CDialog;
- }
-
- // Create new color dialog, store ptr to gWorld in refcon for later disposal
- gModuleDialog = NewCDialog( nil, &theRect, "\pKid's Clicks!",
- false, noGrowDocProc, kMoveToFront,
- true, (long)theNewWorld, module_itemList);
- if (! gModuleDialog)
- {
- theErr = eUnableToOpenFile;
- goto end_GetNew_Module_CDialog;
- }
-
- // Set the module window palette
- if(gPICTPalette != nil)
- SetPalette(gModuleDialog, gPICTPalette, true);
-
- ActivatePalette(gModuleDialog);
- SetGWorld((CGrafPtr)gModuleDialog, nil);
-
- SetGWorld(savedPort, oldDevice);
-
- end_GetNew_Module_CDialog:
- return (theErr);
- }
-
-
- /*********************** Adjust_Module_ToFitScreen ****************************/
-
- void Adjust_Module_ToFitScreen( DialogPtr dialog )
- {
- GDHandle myDevice;
- short iType, iNum, numItems, mBarHeight;
- Handle iHandle;
- Rect iRect, oldPortRect, newPortRect;
- Boolean adjustPosition = true;
-
- myDevice = GetMainDevice();
- newPortRect = (**myDevice).gdRect;
- if ((newPortRect.right > kMaxModuleWidth)
- || (newPortRect.bottom > kMaxModuleHeight))
- {
- SetRect( &newPortRect, 0, 0, kMaxModuleWidth, kMaxModuleHeight);
- adjustPosition = false; // Let it be auto-centered
- }
-
- // Small adjustment so we don't lose part of the picture
- if (adjustPosition)
- {
- mBarHeight = GetMBarHeight();
- newPortRect.bottom -= mBarHeight;
- }
-
- // Save the old port Rect size
- oldPortRect = (dialog->portRect);
-
- // Re-size the port Rect
- (dialog->portRect) = newPortRect;
-
- iNum = CountDITL(dialog);
- if (iNum)
- {
- // Adjust picture's rect
- GetDItem(dialog, iNum, &iType, &iHandle, &iRect);
- SetDItem(dialog, iNum, iType, iHandle, &newPortRect);
-
- // Calculate size increase & Adjust all user items to new screen size
- numItems = iNum;
-
- for (iNum = 1; iNum <= numItems-1; iNum++)
- {
- // ReSize & Calculate Movement of User Item Rects
- GetDItem(dialog, iNum, &iType, &iHandle, &iRect);
- MapRect(&iRect, &oldPortRect, &newPortRect);
- SetDItem(dialog, iNum, iType, iHandle, &iRect);
- }
- }
- PortChanged((GrafPtr)dialog);
-
- if (adjustPosition)
- MoveWindow((WindowPtr)dialog, 0, mBarHeight, false);
- }
-
-
- /*********************** Close_KidsModule ****************************/
-
- void Close_KidsModule( void )
- {
- Handle menu;
- Str255 directionStr;
- GWorldPtr module_gWorld;
-
- module_gWorld = (GWorldPtr)(GetWRefCon(gModuleDialog));
- DisposeGWorld(module_gWorld);
-
- DisposDialog(gModuleDialog);
- DisposCTable(gPICTCTab);
- DisposePalette(gPICTPalette);
-
- CloseResFile(gKidsModuleResNum);
-
- menu = GetMHandle(mApple);
- DelMenuItem(menu, 2);
-
- menu = GetMHandle(mFile);
- DisableItem(menu, iClose);
-
- menu = GetMHandle(mScramble);
- if (gSoundsAreScrambled)
- DisableItem(menu, iUnscramble);
- DisableItem(menu, 0);
- DrawMenuBar();
- gSoundsAreScrambled = false;
-
- menu = GetMHandle(mDirections);
- GetIndString(directionStr, rDirectionStrings, sOpenModule);
- SetItem(menu, 1, directionStr);
-
- gModuleIsLoaded = false;
- }
-
-
- /************************* Handle_KidsClicks **********************************/
-
- void Handle_KidsClicks( EventRecord *eventPtr )
- {
- short itemHit;
-
- SetPort(gModuleDialog);
-
- if( DialogSelect( eventPtr, &gModuleDialog, &itemHit ) )
- PlaySound(itemHit);
- }
-
-
- /************************* PlaySound *******************************/
-
- void PlaySound( short whichSound )
- {
- Handle theSound;
- short mySound;
-
- if (gSoundsAreScrambled)
- mySound = rExpectedSndID + gSoundOrder[whichSound] -1;
- else
- mySound = rExpectedSndID + whichSound - 1;
-
- theSound = GetResource( 'snd ', mySound );
-
- if (theSound)
- {
- SndPlay( NULL, theSound, 0 );
- FlushEvents( mDownMask, 0 );
- }
-
- ReleaseResource(theSound);
- }
-
-
- /*********************** ScrambleSoundOrder *****************************/
-
- void ScrambleSoundOrder( void )
- {
- short numSounds, sndCount, saveResFile;
- unsigned short randomNum;
- short newSoundOrder[255];
- long result, range;
- MenuHandle menu;
-
- saveResFile = CurResFile();
-
- UseResFile(gKidsModuleResNum);
- numSounds = Count1Resources('snd ');
-
- GetDateTime(&randSeed);
-
- // Initialize array of numbers 1 - numSounds in consecutive order
- for (sndCount = 1; sndCount <= numSounds; sndCount++)
- {
- newSoundOrder[sndCount] = sndCount;
- }
-
- // Create a new order by picking a number from the array at random
- // Then shrinking the pot by 1 until only one is left.
- for (sndCount = numSounds; sndCount > 1; sndCount--)
- {
- randomNum = Random();
- range = sndCount;
- result = (randomNum * range) / 65536 + 1;
-
- gSoundOrder[sndCount] = newSoundOrder[result];
-
- // Remove element from array so it isn't repeated
- if (result < sndCount)
- {
- memmove(&(newSoundOrder[result]), &(newSoundOrder[result+1]),
- (sndCount - result) * sizeof(short));
- }
- }
- gSoundOrder[1] = newSoundOrder[1];
-
- if (! gSoundsAreScrambled)
- {
- gSoundsAreScrambled = true;
- menu = GetMHandle(mScramble);
- EnableItem(menu, iUnscramble);
- }
- UseResFile(saveResFile);
- }
-
-
- /************************ Do_AboutDialog ***************************/
-
- void Do_AboutDialog( void )
- {
- DialogPtr dialog;
- Ptr dStorage;
- Boolean done = false;
- GrafPtr savePort;
- Rect itemRect;
- short itemHit, itemType;
- Handle itemHandle, theSound;
-
- GetPort(&savePort);
- theSound = GetResource('snd ', rAboutSnd);
-
- // I allocate my own space to prevent my sound from
- // crashing memory when it is loaded. I am still learning about
- // the Mac's expectation in this area.
- dStorage = NewPtrClear( kAboutDlgMem );
- dialog = GetNewDialog( rAboutDlgID, dStorage, kMoveToFront );
- if (! dialog)
- {
- SysBeep(10);
- SysBeep(10);
- DisposPtr(dStorage);
- goto noDice;
- }
- SetPort(dialog);
-
- GetDItem( dialog, ok, &itemType, &itemHandle, &itemRect);
- SetDialogDefaultItem( dialog, ok );
-
- GetDItem( dialog, iAllieFrame, &itemType, &itemHandle, &itemRect);
- SetDItem(dialog, iAllieFrame, itemType, rectProc, &itemRect);
-
- ShowWindow(dialog);
-
- while (! done){
- ModalDialog(kAboutScreenFilter, &itemHit);
- if (itemHit == iAllieFrame)
- if (theSound)
- SndPlay( NULL, theSound, 0 );
-
- if (itemHit == ok)
- done = true;
- }
-
- CloseDialog(dialog);
- DisposPtr(dStorage);
- noDice:
- ReleaseResource(theSound);
- SetPort(savePort);
- }
-
-
- /************************ Do_AboutModuleDlg ***************************/
-
- void Do_AboutModuleDlg( void )
- {
- DialogPtr dialog;
- Boolean done = false;
- Ptr dStorage;
- GrafPtr savePort;
- Rect itemRect;
- short itemHit, itemType;
- Handle itemHandle;
-
- GetPort(&savePort);
- dStorage = NewPtrClear( kAboutDlgMem );
- dialog = GetNewDialog( rAboutModuleID, dStorage, kMoveToFront );
- if (! dialog)
- {
- SysBeep(10);
- SysBeep(10);
- goto noDice;
- }
- SetPort(dialog);
- GetDItem( dialog, ok, &itemType, &itemHandle, &itemRect);
- SetDialogDefaultItem( dialog, ok );
-
- ShowWindow(dialog);
-
- while (! done){
- ModalDialog(kAboutScreenFilter, &itemHit);
- if (itemHit == ok)
- done = true;
- }
-
- CloseDialog(dialog);
- DisposPtr(dStorage);
- noDice:
- SetPort(savePort);
- }
-
-
- /****************** AboutScreenFilter ******************************/
-
- pascal Boolean AboutScreenFilter( DialogPtr dialog, EventRecord *event,
- short *itemHit )
- {
- Boolean returnVal = false;
- WindowPtr temp;
- ProcPtr *standardProc;
-
- if ( (event->what == updateEvt) && ((WindowPtr)event->message != dialog) )
- {
- returnVal = DoUpdate((WindowPtr)event->message);
- }
- else{
- GetPort(&temp);
- SetPort(dialog);
- GetStdFilterProc(&standardProc);
- returnVal = ((ModalFilterProcPtr)standardProc)
- (dialog, event, itemHit );
-
- SetPort(temp);
- }
- return returnVal;
- }
-
-
- /******************** rectProc *************************/
-
- pascal void rectProc( DialogPtr theDialog, short item )
- {
- short type;
- Rect theRect;
- Handle itemHandle;
-
- GetDItem( theDialog, item, &type, &itemHandle, &theRect);
- PenSize(3,3);
- FrameRect( &theRect );
- PenNormal();
- }
-
-
- /******************* DoOpenApp **************************/
-
- pascal OSErr DoOpenApp ( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon )
- {
- return (noErr);
- }
-
-
- /********************** DoOpenDoc ****************************/
-
- // This code is essentially derived from code included on the CD with
- // "Ultimate Mac Programming", by Dave Mark, MacWorld books.
-
- pascal OSErr DoOpenDoc( const AppleEvent *theAppleEvent,
- AppleEvent *reply, long refCon )
- {
- FSSpec file;
- AEDescList docList;
- long i;
- long itemsInList;
- Size actualSize;
- AEKeyword keyword;
- DescType returnedType;
- OSErr theErr;
-
- // get the direct parameter--a descriptor list--and put it into docList
- theErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
- if (theErr == noErr)
- {
- if (docList.descriptorType != typeAEList)
- {
- AEDisposeDesc (&docList);
- theErr = paramErr;
- }
- else
- {
- // count the number of descriptor records in the list
- theErr = AECountItems(&docList, &itemsInList);
- if (theErr == noErr)
- {
- //get each descriptor record from the list, coerce the returned data
- //to an FSSpec and open the associated file
-
- i = 1;
- if (itemsInList > i)
- DoError(eCantOpenMultiModules);
-
- theErr = AEGetNthPtr(&docList, i, typeFSS, &keyword, &returnedType,
- &file, sizeof(file), &actualSize);
- if (theErr == noErr)
- {
- Open_KidsModule(&file);
- }
- }
- AEDisposeDesc (&docList);
- }
- }
- return (theErr);
- }
-
-
- /************************ DoPrintDoc ***************************/
-
- pascal OSErr DoPrintDoc ( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon )
- {
- return (noErr);
- }
-
-
- /********************* DoQuitApp *****************************/
-
- pascal OSErr DoQuitApp ( AppleEvent theAppleEvent,
- AppleEvent reply, long refCon )
- {
- if (gModuleIsLoaded)
- Close_KidsModule();
- gDone = true;
- return (noErr);
- }
-
-
- /******************** DoError *******************************/
-
- void DoError( short err )
- {
- Str255 errString;
-
- GetIndString( errString, rErrStringsID, err );
-
- ParamText( errString, sEmptyString, sEmptyString, sEmptyString );
-
- if ( gFatal )
- {
- gDone = true;
- StopAlert(rErrorAlertID, nil);
- }
- else
- CautionAlert(rErrorAlertID, nil );
-
- }
-
-
- /********************* CheckSystemEnvironment ***************************/
-
- Boolean CheckSystemEnvironment( void )
- {
- Boolean environmentOK = true;
- long response;
- GDHandle myDevice;
- PixMapHandle monitorPMapH;
- short depth;
-
- /* Kid's Clicks requires System 7 & at least 256 Colors To Operate */
-
- /* Check System Version */
- /* THINK C provides glue code for pre-Sys7 users,
- allowing me to use Gestalt here */
- Gestalt(gestaltSystemVersion, &response);
- if (response < kSystemSeven)
- {
- environmentOK = false;
- goto checkDone;
- }
-
- /* Check QuickDraw version */
- Gestalt(gestaltQuickdrawVersion, &response);
- if (response >= gestalt8BitQD)
- {
- myDevice = GetMainDevice();
- if (HasDepth(myDevice,8,1,1)) // Does device support 256 Colors?
- {
- /* Check Monitor Current Monitor Depth */
- monitorPMapH = (**myDevice).gdPMap;
- depth = (**monitorPMapH).pixelSize;
- if (depth < k256ColorDepth)
- environmentOK = false;
- }
- }
- else
- environmentOK = false; /* Machine does not support 256 Colors */
-
- checkDone:
- return (environmentOK);
- }
-
-
- /************************* Pstrcpy *********************************/
-
- void Pstrcpy( Str255 *destString, Str255 srcString)
- {
- PtoCstr(destString);
- PtoCstr(srcString);
- strcpy(destString, srcString);
- CtoPstr(destString);
- CtoPstr(srcString);
- }